package com.example.coursehub.classmanagement.controller;

import com.example.coursehub.classmanagement.entity.Class;
import com.example.coursehub.classmanagement.entity.StudentClass;
import com.example.coursehub.classmanagement.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 班级管理控制器
 */
@RestController
@RequestMapping("/api/classes")
@CrossOrigin(origins = "*")
public class ClassController {
    
    @Autowired
    private ClassService classService;
    
    // 班级相关接口
    @PostMapping
    public ResponseEntity<Class> createClass(@RequestBody Class clazz) {
        Class createdClass = classService.createClass(clazz);
        return ResponseEntity.ok(createdClass);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<Class> updateClass(@PathVariable Long id, @RequestBody Class clazz) {
        try {
            Class updatedClass = classService.updateClass(id, clazz);
            return ResponseEntity.ok(updatedClass);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteClass(@PathVariable Long id) {
        try {
            classService.deleteClass(id);
            return ResponseEntity.ok().build();
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<Class> getClassById(@PathVariable Long id) {
        Optional<Class> clazz = classService.getClassById(id);
        return clazz.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping
    public ResponseEntity<List<Class>> getAllClasses() {
        List<Class> classes = classService.getAllClasses();
        return ResponseEntity.ok(classes);
    }
    
    @GetMapping("/course/{courseId}")
    public ResponseEntity<List<Class>> getClassesByCourse(@PathVariable Long courseId) {
        List<Class> classes = classService.getClassesByCourse(courseId);
        return ResponseEntity.ok(classes);
    }
    
    @GetMapping("/teacher/{teacherId}")
    public ResponseEntity<List<Class>> getClassesByTeacher(@PathVariable Long teacherId) {
        List<Class> classes = classService.getClassesByTeacher(teacherId);
        return ResponseEntity.ok(classes);
    }
    
    @GetMapping("/search")
    public ResponseEntity<List<Class>> searchClasses(@RequestParam String name) {
        List<Class> classes = classService.searchClassesByName(name);
        return ResponseEntity.ok(classes);
    }
    
    @GetMapping("/available")
    public ResponseEntity<List<Class>> getAvailableClasses() {
        List<Class> classes = classService.getAvailableClasses();
        return ResponseEntity.ok(classes);
    }
    
    // 学生班级关联接口
    @PostMapping("/{classId}/students/{studentId}")
    public ResponseEntity<StudentClass> joinClass(@PathVariable Long classId, @PathVariable Long studentId) {
        try {
            StudentClass studentClass = classService.joinClass(studentId, classId);
            return ResponseEntity.ok(studentClass);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().build();
        }
    }
    
    @DeleteMapping("/{classId}/students/{studentId}")
    public ResponseEntity<Void> leaveClass(@PathVariable Long classId, @PathVariable Long studentId) {
        try {
            classService.leaveClass(studentId, classId);
            return ResponseEntity.ok().build();
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().build();
        }
    }
    
    @GetMapping("/{classId}/students")
    public ResponseEntity<List<StudentClass>> getStudentsByClass(@PathVariable Long classId) {
        List<StudentClass> students = classService.getStudentsByClass(classId);
        return ResponseEntity.ok(students);
    }
    
    @GetMapping("/students/{studentId}")
    public ResponseEntity<List<StudentClass>> getClassesByStudent(@PathVariable Long studentId) {
        List<StudentClass> classes = classService.getClassesByStudent(studentId);
        return ResponseEntity.ok(classes);
    }
    
    @GetMapping("/{classId}/students/{studentId}/check")
    public ResponseEntity<Boolean> checkStudentInClass(@PathVariable Long classId, @PathVariable Long studentId) {
        boolean isInClass = classService.isStudentInClass(studentId, classId);
        return ResponseEntity.ok(isInClass);
    }
    
    // 统计接口
    @GetMapping("/{classId}/students/count")
    public ResponseEntity<Long> getActiveStudentCount(@PathVariable Long classId) {
        long count = classService.getActiveStudentCount(classId);
        return ResponseEntity.ok(count);
    }
    
    @GetMapping("/teacher/{teacherId}/count")
    public ResponseEntity<Long> getClassCountByTeacher(@PathVariable Long teacherId) {
        long count = classService.getClassCountByTeacher(teacherId);
        return ResponseEntity.ok(count);
    }
    
    @GetMapping("/course/{courseId}/count")
    public ResponseEntity<Long> getClassCountByCourse(@PathVariable Long courseId) {
        long count = classService.getClassCountByCourse(courseId);
        return ResponseEntity.ok(count);
    }
}